﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Reflection;
using IO = System.IO;
using System.Threading;
using System.Windows.Threading;
using TheMovieDBV3;
using MOJsdk;
using Diag = System.Diagnostics;
using Microsoft.Win32;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IHost
    {
        #region PrivateVars
        private string _local;
        private string _search = "";
        private MojSystem _system;
        private MovingElement _moving_element;
        #endregion

        #region DynamicControls
        private MenuControl _mainMenu;
        private Control_Toasts _Ctrl_Toasts;
        #endregion

        #region Inicialization
        public MainWindow()
        {
            InitializeComponent();
            INI();
        }
        public void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _system.Plugins.IniPlugins();
            LoadCompleted(this);
            //StartTest();
        }

        private void INI()
        {
            MojHost.GetHost = this;

            INISettings();

            if(MojEnviromentVarsEnum.DeveloperMode.Value())
                INIDeveloperMode();

            INIDynamicControls();
            INIToolButtons();
            ResizeControls();
            INIDefaultSettings();
            INIHotKeys();
            INIEvents();
            INIStyle();
            INIRegistration();
        }

        private void INISettings()
        {
            _local = IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            _system = new MojSystem(_local, this);
            _moJTabItems = new List<MojSettingsTabControItem>();
            _system.INI();
            MojStatusVarsEnum.DisplayPanel.Set(new EmptyPanel());

            #region IHostEvents
            this.SelectedFileChanged += delegate { };
            this.SelectedMovieChanged += delegate { };
            this.DisplayPanelChanged += OnDisplayPanelChanges;
            this.LoadCompleted += delegate { };
            this.OnError += delegate { };
            this.OnStyleCalled += delegate { };
            #endregion
        }
        private void INIDeveloperMode()
        {
            if (MojEnviromentVarsEnum.ShowStyleInspector.Value())
            {
                StyleInspector winStye = new StyleInspector();
                winStye.Show();
                winStye.Set(this);
            }
        }
        private void INIDynamicControls()
        {
            #region Toasts
            _Ctrl_Toasts = new Control_Toasts();
            _canvas_mainwin.Children.Add(_Ctrl_Toasts);
            Canvas.SetLeft(_Ctrl_Toasts, 1);
            Canvas.SetBottom(_Ctrl_Toasts, 0);
            Canvas.SetZIndex(_Ctrl_Toasts, 5000);
            #endregion

            #region MainMenu
            _mainMenu = new MenuControl();
            _mainMenu.ButtonHeight = 25;
            _mainMenu.ButtonWidth = 90;

            #region PositionOnCanvas
            _canvas_mainwin.Children.Add(_mainMenu);
            Canvas.SetLeft(_mainMenu, 10);
            Canvas.SetTop(_mainMenu, 30);
            Canvas.SetZIndex(_mainMenu, 90);
            _mainMenu.Visibility = System.Windows.Visibility.Hidden;
            #endregion
            #region Settings
            Button btnsettings = new Button();
            btnsettings.Content = "Settings";
            btnsettings.Click += delegate
            {
                if (MojStatusVarsEnum.IsSettingsWindowVisible.Value())
                    return;
                else
                {
                    Control_MainSettings sett = new Control_MainSettings();
                    sett.Width = 500;
                    sett.Height = 500;

                    WindowControlInfo info = new WindowControlInfo(500, 500);
                    info.WindowTitle = "Settings";
                    info.DefaultPosition = WindowPosition.Center;

                    WindowControl ctrl = AddNewUserControl(sett, info);
                    ctrl.OnClosed += delegate { MojStatusVarsEnum.IsSettingsWindowVisible.Set(false); };

                    MojStatusVarsEnum.IsSettingsWindowVisible.Set(true);
                }
            };
            btnsettings.ApplyStyle("0101011");

            _mainMenu.AddNewButton(btnsettings);

            #endregion
            #region ExitButton
            Button btnExit = new Button();
            btnExit.Content = "Exit";
            btnExit.Click += delegate 
            {
                this.Close();
            };
            btnExit.ApplyStyle("0101012");

            _mainMenu.AddNewButton(btnExit);
            #endregion
            #endregion

            #region TopBar
            _stackPanel_mainwin_topbar.Orientation = Orientation.Horizontal;
            #endregion
        }
        private void INIToolButtons()
        {
            _toolButtons = new MojToolButtonCollection();

            #region RemoveMovie
            MojToolButton BtnRemoveMovie = new MojToolButton("R", "0301002", "Remove movie from library", null);
            BtnRemoveMovie.Click += RemoveMovieToolButtonClicked;
            BtnRemoveMovie.AddLogicStatment(new LogicStatment()
            {
                Code = MojToolButtonShowCondition.IPanel_ManageMovies.Code + " && " + MojToolButtonShowCondition.IsMovieSelected.Code,
                Librarys = "",
            });
            _toolButtons.Add(BtnRemoveMovie);
            #endregion
            #region RemoveAllMovies
            MojToolButton BtnRemoveAll = new MojToolButton("RA", "0301006", "Remove all movies", null);
            BtnRemoveAll.Click += RemoveAllMoviesToolButtonClicked;
            BtnRemoveAll.AddLogicStatment(MojToolButtonShowCondition.IPanel_ManageMovies);
            _toolButtons.Add(BtnRemoveAll);
            #endregion
        }
        private void INIDefaultSettings()
        {
            AddNewSettingsTabItem(MOJLocalization.Localize("Plugins"), new Control_PluginsSettings());

            //Add Style Settings
            if (MojHost.GetHost.HostStyle != null)
                if (MojHost.GetHost.HostStyle.StyleSettings != null)
                    AddNewSettingsTabItem(MOJLocalization.Localize("Style"), MojHost.GetHost.HostStyle.StyleSettings);
        }
        private void INIEvents()
        {
            try
            {
                _moving_element = null;

                this.Loaded += MainWindow_Loaded;
                this.SizeChanged += ResizeControls;
                _btn_mainwin_mainmenu.Click += ShowMainMenu;
                _mainMenu.MouseLeave += MouseLeaveMainMenu;
                //_btn_mainwin_mainmenu.LostFocus += MainMenuLostFocus;
                _canvas_mainwin.MouseMove += MoveElement;

            }
            catch (Exception TheEx)
            {
                ErrorReporter reporter = new ErrorReporter()
                {
                    ErrorCode = "INIEventsX0",
                    ErrorMessage = "Error Resizing Controls",
                    ErrorDetails = TheEx.ToString()
                };
            }
        }
        private void INIHotKeys()
        {

        }
        private void INIStyle()
        {
            _canvas_mainwin.ApplyStyle("0101006");
            _btn_mainwin_mainmenu.ApplyStyle("0101007");
            _dockPanel.ApplyStyle("0101008");
            _stackPanel_mainwin_topbar.ApplyStyle("0101009");
            _Grid_Foreground.ApplyStyle("0101054");

            _Grid_Foreground.Visibility = System.Windows.Visibility.Hidden;
        }
        private void INIRegistration()
        {

        }

        private void StartTest()
        {
            AddNewUserControl(new Control_Test(), new WindowControlInfo(300, 160)
            {
                DefaultPosition = WindowPosition.Center,
            });
        }
        #endregion

        #region VisualMethods
        //Set position of controls on screen acording to screen size
        private void ResizeControls(object sender, SizeChangedEventArgs e)
        { ResizeControls(); }
        private void ResizeControls()
        {
            try
            {
                double cHeight = _canvas_mainwin.ActualHeight;
                double cWidth = _canvas_mainwin.ActualWidth;

                _dockPanel.Width = cWidth - 20;
                _dockPanel.Height = cHeight - 50;

                _stackPanel_mainwin_topbar.Width = cWidth - 70;

                _Grid_Foreground.Width = cWidth;
                _Grid_Foreground.Height = cHeight;

                _Ctrl_Toasts.Width = cWidth;
                _Ctrl_Toasts.Height = cHeight / 2;
                _Ctrl_Toasts.SetMaxElems((int)(cHeight / 2) / 23);
            }
            catch (Exception TheEx)
            {
                ErrorReporter reporter = new ErrorReporter()
                {
                    ErrorCode = "ResizeControls",
                    ErrorMessage = "Error Resizing Controls",
                    ErrorDetails = TheEx.ToString()
                };
            }
        }
        #endregion

        #region IHost
        #region Public Elements
        public Canvas HostCanvas
        { get { return _canvas_mainwin; } }
        public ResourceDictionary HostResources
        { get { return this.Resources; } }
        public Window TheMainWindow
        { get { return this; } }
        public MojSystem MoJSystem
        { get { return _system; } }
        public MenuControl MainMenu
        { get { return _mainMenu; } }
        #endregion

        #region ToolButtons
        public StackPanel TopToolBar
        { get { return _stackPanel_mainwin_topbar; } }
        public MojToolButtonCollection ToolButtonCollection
        { get { return _toolButtons; } }
        #endregion

        #region Actions
        public void ActionFocus()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
                this.Activate();
            });
        }
        public void SelectNextMovie()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                if (MojStatusVarsEnum.DisplayPanel.Value() != null)
                    MojStatusVarsEnum.DisplayPanel.Value().SelectNext();
            });
        }
        public void SelectPrevMovie()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                if (MojStatusVarsEnum.DisplayPanel.Value() != null)
                    MojStatusVarsEnum.DisplayPanel.Value().SelectPrev();
            });
        }
        public void ShowBrowseMovies()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
            });

        }
        public void ShowManageMovies()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {

            });
        }
        public void ShowSettings()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
                if (MojStatusVarsEnum.IsSettingsWindowVisible.Value())
                    return;
                else
                {
                    Control_MainSettings sett = new Control_MainSettings();
                    sett.Width = 500;
                    sett.Height = 500;

                    WindowControlInfo info = new WindowControlInfo(500, 500);
                    info.WindowTitle = "Settings";
                    info.DefaultPosition = WindowPosition.Center;

                    WindowControl ctrl = AddNewUserControl(sett, info);
                    ctrl.OnClosed += delegate { MojStatusVarsEnum.IsSettingsWindowVisible.Set(false); };

                    MojStatusVarsEnum.IsSettingsWindowVisible.Set(true);
                }
            });
        }
        public void PlaySelectedMovie()
        {
            if (MojStatusVarsEnum.IsMovieSelected.Value())
            {
                MojMovie m = new MojMovie(MojStatusVarsEnum.SelectedMovie.Value());

                if(m.IsAvaible && m.MovieFiles.Count > 0)
                    PlayFile(m.MovieFiles[0].FileFullPath);
            }
        }
        #endregion

        public void PlayFile(string FilePath)
        {
            if (IO.File.Exists(FilePath))
            {
                Diag.Process p = new Diag.Process();
                p.StartInfo = new Diag.ProcessStartInfo(FilePath);
                p.Start();
            }
        }
        public void OpenFolder(string Path)
        {
            if (IO.File.Exists(Path))
            {
                string folder = IO.Path.GetDirectoryName(Path);
                string argument = string.Format("\"{0}\"", folder);
                System.Diagnostics.Process.Start("explorer.exe", argument);
            }
        }

        public void SetMoviePanel(IPanel Panel)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                if (Panel == null) return;
                if (Panel.PanelIdentifier == MojStatusVarsEnum.DisplayPanel.Value().PanelIdentifier) return;

                IPanel temp = MojStatusVarsEnum.DisplayPanel.Value();

                _dockPanel.Children.Clear();
                MojStatusVarsEnum.DisplayPanel.Set(Panel);
                MojStatusVarsEnum.DisplayPanel.Value().OnLoadComplete += delegate { };
                MojStatusVarsEnum.DisplayPanel.Value().OnSelectedFileChanged += delegate { };
                MojStatusVarsEnum.DisplayPanel.Value().OnSelectedMovieChanged += delegate { };

                if (Panel.Control != null)
                {
                    _dockPanel.Children.Add(Panel.Control);
                    Panel.LoadMovies();
                }

                CallEventDisplayPanelChanged(this, new DisplayPanelChangedArgs() 
                    { NewPanel = MojStatusVarsEnum.DisplayPanel.Value(), OriginalPanel = temp, });
            });
        }
        public void SetMetaDataControl(IMetaData CtrlMetaData)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                if (CtrlMetaData == null) return;

                _dockPanel.Children.Clear();
                MojStatusVarsEnum.MetaDataControl.Set(CtrlMetaData);
                MojStatusVarsEnum.MetaDataControl.Value().OnMovieInfoUpdated += delegate { };
            });
        }

        public void ReportError(ErrorReporter Reporter)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
                if (MojStatusVarsEnum.ShowSystemErrorReporter.Value())
                {
                    MessageBox.Show(Reporter.ErrorDetails, Reporter.ErrorCode);
                }

                OnError(this, Reporter);
            });
        }
        public WindowControl AddNewUserControl(UserControl Control, WindowControlInfo Info)
        {
            WindowControl ctrl = new WindowControl(Control, Info);
            Control.Height = double.NaN;
            Control.Width = double.NaN;
            ctrl.Width = Info.WindowWidth;
            ctrl.Height = Info.WindowHeight;
            ctrl.HeaderRectangle.MouseLeftButtonDown +=
                delegate { _moving_element = new MovingElement(ctrl); };
            ctrl.HeaderRectangle.MouseLeftButtonUp += delegate { _moving_element = null; };
            ctrl.OnClosed += delegate { _canvas_mainwin.Children.Remove(ctrl); };

            _canvas_mainwin.Children.Add(ctrl);
            Canvas.SetLeft(ctrl, Info.GetElementPosition().Left);
            Canvas.SetTop(ctrl, Info.GetElementPosition().Top);
            Canvas.SetZIndex(ctrl, Info.ZIndex);

            return ctrl;
        }
        public int SelectOptions(string WinTitle, string[] Options)
        {
            Win_UserOption win = new Win_UserOption(WinTitle, Options);

            if (win.ShowDialog().Value)
            {
                return win.Result;
            }

            return -1;
        }
        public void AddToasts(string Text)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                _Ctrl_Toasts.Add(Text);
            });
        }
        public void ShowMessage(string Message, string Title)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                MessageBox.Show(Message, Title);
            });
        }
        public void Search(string query)
        {
            try
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
                {

                });
            }
            catch (Exception ex)
            {
                ReportError(new ErrorReporter()
                {
                    ErrorCode = "SEARCHERROR1",
                    ErrorDetails = ex.ToString(),
                    ErrorMessage = "Error searching string",
                });
            }
        }
        public void ResetElements()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {

            });
        }
        public void SetForegroundGrid(Visibility GridVisibility)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
                if (GridVisibility == System.Windows.Visibility.Collapsed)
                    GridVisibility = System.Windows.Visibility.Hidden;

                _Grid_Foreground.Visibility = GridVisibility;
            });
        }
        public void ReloadElementsInfo()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
                MojStatusVarsEnum.DisplayPanel.Value().LoadMovies();
            });
        }
        public void ShowMovieDetails(string MovieCode)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                if (MojStatusVarsEnum.MetaDataControl.Value() == null) return;
                if (MojStatusVarsEnum.MetaDataControl.Value().Control == null) return;
                if (MojStatusVarsEnum.IsMovieDetailsWindowVisible.Value())
                {
                    MojStatusVarsEnum.MetaDataControl.Value().LoadMovie(MovieCode);
                    return;
                }

                IMetaData data = MojStatusVarsEnum.MetaDataControl.Value();
                data.LoadMovie(MovieCode);

                WindowControl ctrl = AddNewUserControl(data.Control, new WindowControlInfo(data.Width, data.Height) 
                    { WindowTitle = MOJLocalization.Localize("Movie Details") });
                MojStatusVarsEnum.IsMovieDetailsWindowVisible.Set(true);
                ctrl.OnClosed += delegate { MojStatusVarsEnum.IsMovieDetailsWindowVisible.Set(false); };

                data.LoadMovie(MovieCode);
            });
        }

        #region Styles
        public IStyle HostStyle 
        { 
            get 
            {
                string name = MojEnviromentVarsEnum.SkinFile.Value();
                foreach (var v in GetStyles())
                    if (v.StyleName == name)
                        return v;

                return null;
            } 
        }
        public Style LoadStyle(string StyleKeyValue)
        {
            try
            {
                return HostStyle.LoadStyleByKeyValue(StyleKeyValue);
            }
            catch
            {
                return null;
            }
        }
        public Style LoadStyle(ElementInfo Info)
        {
            OnStyleCalled(this, Info);

            try
            {
                return HostStyle.LoadStyle(Info);
            }
            catch
            {
                return null;
            }
        }
        public IList<IStyle> GetStyles()
        {
            List<IStyle> list = new List<IStyle>();

            string folder = MojFoldersEnum.SkinsFolder.Value();
            foreach (string p in IO.Directory.EnumerateFiles(folder, "*.mojstyle", IO.SearchOption.TopDirectoryOnly))
            {
                IStyle style = ImportStyle(p);
                if (style != null)
                    list.Add(style);
            }

            return list;
        }
        public bool StyleExist(IStyle TheStyle)
        {
            if (GetStyles().Where(z => z.StyleName == TheStyle.StyleName).Count() != 0)
                return true;

            return false;
        }
        public void InstallStyle()
        {
            OpenFileDialog diag = new OpenFileDialog();
            diag.Filter = "MojStyle|*.mojstyle";
            diag.InitialDirectory = "C:\\";

            if (diag.ShowDialog().Value)
            {
                string file = diag.FileName;
                IStyle style = ImportStyle(file);
                if (style != null)
                {
                    string dest = IO.Path.Combine(MojFoldersEnum.SkinsFolder.Value(), style.StyleName + ".mojstyle");
                    IO.File.Copy(file, dest);
                    ShowMessage(MOJLocalization.Localize("MoJStyle file installed"), MOJLocalization.Localize("Install Style"));
                }
                else
                {
                    ShowMessage(MOJLocalization.Localize("Invalid MoJStyle file"), MOJLocalization.Localize("Install Style"));
                }
            }
        }
        #endregion

        #region Events
        public event StyleCalledHandlrer OnStyleCalled;
        public event SelectedFileChangedHandler SelectedFileChanged;
        public void CallEventSelectedFileChanged(object sender, SelectedFileChangedArgs e)
        { SelectedFileChanged(sender, e); }
        public event SelectedMovieChangedHandler SelectedMovieChanged;
        public void CallEventSelectedMovieChanged(object sender, SelectedMovieChangedArgs e)
        { SelectedMovieChanged(sender, e); }
        public event DisplayPanelChangedHandler DisplayPanelChanged;
        public void CallEventDisplayPanelChanged(object sender, DisplayPanelChangedArgs e)
        { DisplayPanelChanged(sender, e); }
        public event CallEventHandler LoadCompleted;
        public event ErrorReportHandling OnError;
        #endregion

        #region TabControlSettings
        private List<MojSettingsTabControItem> _moJTabItems;
        public MojSettingsTabControItem AddNewSettingsTabItem(string Title, UserControl TheControl)
        {
            MojSettingsTabControItem itm = new MojSettingsTabControItem()
            {
                ID = _moJTabItems.Count,
                Title = Title,
                Item = TheControl,
            };

            _moJTabItems.Add(itm);
            return itm;
        }
        public IList<MojSettingsTabControItem> GetSettingsTabItems()
        {
            return _moJTabItems;
        }
        public bool RemoveSettingsTabItem(int ID)
        {
            try
            {
                _moJTabItems.RemoveAt(ID);

                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region ElementsRegistation
        public bool ElemNameIsRegistered(string RegName)
        {
            try
            {
                object obj = this.FindName(RegName);
                if (obj != null) return true;

                return false;
            }
            catch
            {
                return false;
            }
        }
        public void RegisterElement(UIElement Elem, string RegName)
        {
            if (ElemNameIsRegistered(RegName))
            {
                ReportError(new ErrorReporter()
                {
                    ErrorCode = "RegElemX001",
                    ErrorDetails = "Name Allready registered",
                    ErrorMessage = "Name Allready registered",
                });
            }

            this.RegisterName(RegName, Elem);
        }
        public object GetElement(string RegName)
        {
            if (ElemNameIsRegistered(RegName))
                return this.FindName(RegName);

            return null;
        }
        public void UnRegisterElement(string RegName)
        {
            if (ElemNameIsRegistered(RegName))
                this.UnregisterName(RegName);
        }
        #endregion

        #endregion

        #region StatusVarsEvents
        public void OnDisplayPanelChanges(object sender, DisplayPanelChangedArgs e)
        {
            _toolButtons.Update();
        }
        #endregion

        #region VisualEvents
        private void ShowMainMenu(object sender, RoutedEventArgs e)
        {
            if (_mainMenu.Visibility == System.Windows.Visibility.Visible)
                _mainMenu.Visibility = System.Windows.Visibility.Hidden;
            else
                _mainMenu.Visibility = System.Windows.Visibility.Visible;
        }
        private void MouseLeaveMainMenu(object sender, MouseEventArgs e)
        {
            _mainMenu.Visibility = System.Windows.Visibility.Hidden;
        }
        private void MoveElement(object sender, MouseEventArgs e)
        {
            try
            {
                if (_moving_element == null) return;

                Point CanvasMouse = Mouse.GetPosition(_canvas_mainwin);
                Point ElemMouse = _moving_element.MousePos;

                double x = CanvasMouse.X - ElemMouse.X;
                double y = CanvasMouse.Y - ElemMouse.Y;

                Canvas.SetLeft(_moving_element.Element, x);
                Canvas.SetTop(_moving_element.Element, y);
            }
            catch (Exception ex)
            {
                ReportError(new ErrorReporter()
                {
                    ErrorCode = "MovElem",
                    ErrorDetails = ex.ToString(),
                    ErrorMessage = "Error moving element",
                });
            }
        }
        #endregion

        #region ToolButtons
        private MojToolButtonCollection _toolButtons;
        private void RemoveMovieToolButtonClicked(object sender, RoutedEventArgs e)
        {

        }
        private void RemoveAllMoviesToolButtonClicked(object sender, RoutedEventArgs e)
        {
            SetForegroundGrid(Visibility.Visible);
            NoArgDelegate th = new NoArgDelegate(RemoveAllMoviesAsync);
            th.BeginInvoke(null, null);
        }
        private void RemoveAllMoviesAsync()
        {
            List<MoJBasicMovie> list = new List<MoJBasicMovie>(_system.MoviesInterop.Movies);
            foreach (var v in list)
            {
                _system.MoviesInterop.RemoveMovie(v.MovieCode);
            }

            SetForegroundGrid(Visibility.Hidden);
            MojHost.GetHost.ReloadElementsInfo();
        }
        #endregion

        #region ExternalInterop
        private IStyle ImportStyle(string FILEPATH)
        {
            try
            {
                Assembly ass = Assembly.LoadFile(FILEPATH);
                foreach (Type t in ass.GetExportedTypes())
                    if (t.GetInterface("IStyle") != null)
                        return Activator.CreateInstance(t) as IStyle;

                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }

    public class MovingElement
    {
        public MovingElement(UIElement Elem)
        {
            Element = Elem;
            MousePos = Mouse.GetPosition(Element);
        }

        public UIElement Element { get; set; }
        public Point MousePos { get; set; }
    }
}
